package com.tywj.buscustomerapp.view.activity;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.tywj.buscustomerapp.R;
import com.tywj.buscustomerapp.model.bean.BuyedBean;
import com.tywj.buscustomerapp.model.bean.DateJoe;
import com.tywj.buscustomerapp.model.bean.RequestBean;
import com.tywj.buscustomerapp.model.bean.StationBean;
import com.tywj.buscustomerapp.model.bean.TravelLineBean;
import com.tywj.buscustomerapp.model.bean.YuPiaoBean;
import com.tywj.buscustomerapp.presenter.BasePresenter;
import com.tywj.buscustomerapp.presenter.BuyTicketPresenter;
import com.tywj.buscustomerapp.presenter.contract.BuyTicketContract;
import com.tywj.buscustomerapp.utils.SPUtils;
import com.tywj.buscustomerapp.utils.TaylorClickLinstener;
import com.tywj.buscustomerapp.utils.TimeRender;
import com.tywj.buscustomerapp.utils.myadapter.CommonRcvAdapter;
import com.tywj.buscustomerapp.utils.myadapter.item.AdapterItem;
import com.tywj.buscustomerapp.view.item.DateItem;
import com.tywj.buscustomerapp.view.item.DateItem1;
import com.tywj.buscustomerapp.view.myview.LoadingDialog;
import com.tywj.buscustomerapp.view.myview.ToastJoe;
import com.umeng.analytics.MobclickAgent;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;

public class BuyTicketsActivity extends BaseActivityMvp implements BuyTicketContract.BuyTicketView{
    @BindView(R.id.head_left) ImageView back;
    @BindView(R.id.head_title) TextView title;
    @BindView(R.id.dateRecycler) RecyclerView dateRecycler;
    @BindView(R.id.ticket_once) LinearLayout onceTicketLayout;
    @BindView(R.id.ticket_once_about) TextView onceTicketAbout;
    @BindView(R.id.location_start) LinearLayout locationStart;
    @BindView(R.id.location_end) LinearLayout locationEnd;
    @BindView(R.id.check_ticktes) TextView checkTicketNumber;
    @BindView(R.id.buyticket_checkline1) LinearLayout checkLine1;
    @BindView(R.id.start) TextView start;
    @BindView(R.id.end) TextView end;
    @BindView(R.id.line_gry) View lineGry;
    @BindView(R.id.total_price) TextView total_price;
    @BindView(R.id.submit) TextView submit;
    @BindView(R.id.month_ticket_time) TextView month_time;
    @BindView(R.id.add) ImageView add;
    @BindView(R.id.low) ImageView low;
    @BindView(R.id.per_number) TextView perNumber;

    private BuyTicketContract.BuyTicketPresenter presenter = new BuyTicketPresenter(this);
    private OptionsPickerView tvStart;
    private OptionsPickerView tvEnd;
    private CommonRcvAdapter<DateJoe> adapter;
    private List<DateJoe> dateJoes = new ArrayList<>();//日历
    private List<DateJoe> checkTickets=new ArrayList<>();//已选择的日期
    private TravelLineBean travelLineBean;//线路
    private DateItem1 item;
    private List<StationBean> stations = new ArrayList<>();//所有站点
    private List<String> startStations = new ArrayList<>();//开始站点s
    private List<String> startStationsId = new ArrayList<>();//开始站点Ids
    private List<String> endStations = new ArrayList<>();//结束站点s
    private List<String> endStationsId = new ArrayList<>();//结束站点ids
    private String chooseStart ="";//选择的起始站点
    private String chooseStartId ="";//选择的起始站点id
    private String chooseEnd ="";//选择的结束站点
    private String chooseEndId ="";//选择的结束站点id
    private LoadingDialog waitDialog;
    private int postition = 0;
    private String price = "0";//显示的最终价格
    public static BuyTicketsActivity activity;
    private int number = 1;


    private void drewDialog() {
        tvStart = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                if (startStations.size()>0){
                    chooseStart = startStations.get(options1);
                    chooseStartId = startStationsId.get(options1);
                    postition = options1;
                    start.setText(chooseStart);
                    if (chooseEnd!=null && !chooseEnd.equals("")){
                        if (endStationsId.contains(chooseStartId) && (endStationsId.indexOf(chooseStartId)>=endStationsId.indexOf(chooseEndId))){
                            chooseEnd = "";
                            chooseEndId= "";
                            end.setText(R.string.ticket_end);
                            ClearEndStation();
                            price = "0";
                            checkTickets.clear();
                            ChangeToatalMoney();
                            dateJoes = TimeRender.getThisWeekDate1(new Date());
                        }else{
                            if (travelLineBean!=null && !travelLineBean.getType().equals("CommuteLine"))
                                presenter.startGetPrice(travelLineBean.getNumber(), chooseStartId, chooseEndId);
                        }
                    }
                }
            }
        }).setSubCalSize(15)
                .isDialog(true)
                .setSubmitColor(getResources().getColor(R.color.appColor))//确定按钮文字颜色
                .setCancelColor(getResources().getColor(R.color.gray1))//取消按钮文字颜色
                .build();
        tvEnd = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                if (!(options1>=endStations.size()) &&endStations.get(options1)!=null) {
                    chooseEnd = endStations.get(options1);
                    chooseEndId = endStationsId.get(options1);
                    end.setText(chooseEnd);
                    if (travelLineBean!=null && !travelLineBean.getType().equals("CommuteLine"))
                        presenter.startGetPrice(travelLineBean.getNumber(), chooseStartId, chooseEndId);
                }else{
                    chooseEnd = endStations.get(endStations.size()-1);
                    chooseEndId = endStationsId.get(endStationsId.size()-1);
                    end.setText(chooseEnd);
                    if (travelLineBean!=null && !travelLineBean.getType().equals("CommuteLine"))
                        presenter.startGetPrice(travelLineBean.getNumber(), chooseStartId, chooseEndId);
                }
            }
        }).setSubCalSize(15)
                .isDialog(true)
                .setSubmitColor(getResources().getColor(R.color.appColor))//确定按钮文字颜色
                .setCancelColor(getResources().getColor(R.color.gray1))//取消按钮文字颜色
                .build();
        Dialog mDialog = tvStart.getDialog();
        Dialog mDialog1 = tvEnd.getDialog();
        if (mDialog != null && mDialog1!=null) {
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.BOTTOM);
            params.leftMargin = 0;
            params.rightMargin = 0;
            tvStart.getDialogContainerLayout().setLayoutParams(params);
            tvEnd.getDialogContainerLayout().setLayoutParams(params);

            Window dialogWindow = mDialog.getWindow();
            if (dialogWindow != null) {
                dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
                WindowManager.LayoutParams lp = dialogWindow.getAttributes();
                lp.width = WindowManager.LayoutParams.MATCH_PARENT;
                lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
                dialogWindow.setAttributes(lp);
                dialogWindow.setWindowAnimations(com.bigkoo.pickerview.R.style.picker_view_slide_anim);//修改动画样式
                dialogWindow.setGravity(Gravity.BOTTOM);//改成Bottom,底部显示
            }
            Window dialogWindow1 = mDialog1.getWindow();
            if (dialogWindow1 != null) {
                dialogWindow1.getDecorView().setPadding(0, 0, 0, 0);
                WindowManager.LayoutParams lp = dialogWindow1.getAttributes();
                lp.width = WindowManager.LayoutParams.MATCH_PARENT;
                lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
                dialogWindow1.setAttributes(lp);
                dialogWindow1.setWindowAnimations(com.bigkoo.pickerview.R.style.picker_view_slide_anim);//修改动画样式
                dialogWindow1.setGravity(Gravity.BOTTOM);//改成Bottom,底部显示
            }
        }
        tvStart.setPicker(startStations, null, null);
        tvEnd.setPicker(endStations, null, null);
    }

    private void drewDatePicket() {
        adapter = new CommonRcvAdapter<DateJoe>(dateJoes) {
            @NonNull
            @Override
            public AdapterItem createItem(Object type) {
                item = new DateItem1();
                item.setListener(new DateItem1.OnCheckListener() {
                    @Override
                    public void onChecked(DateJoe dateJoe,int pos,View view) {
                        if (chooseStartId.equals("")){
                            ToastJoe.getmToastJoe().ToastShow(BuyTicketsActivity.this,null,"请选择上车站点");
                            dateJoe.isCheck = false;
                            adapter.notifyItemChanged(pos,dateJoe);
                        }else if (chooseEndId.equals("")){
                            ToastJoe.getmToastJoe().ToastShow(BuyTicketsActivity.this,null,"请选择下车站点");
                            dateJoe.isCheck = false;
                            adapter.notifyItemChanged(pos,dateJoe);
                        }
                        else{
                            //这里不再调用check接口
                            presenter.startCheckTicket(pos,dateJoe,travelLineBean.getNumber(),chooseStartId,chooseEndId);
                        }
                    }

                    @Override
                    public void onDeleted(DateJoe dateJoe) {
                        checkTickets.remove(dateJoe);
                        ChangeToatalMoney();
                    }
                });
                return item;
            }
        };
        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this,7);
        dateRecycler.setLayoutManager(layoutManager);
        dateRecycler.setAdapter(adapter);
        dateJoes = TimeRender.getThisWeekDate1(new Date());

    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_but_ticket;
    }

    @Override
    public void initView() {
        title.setText("购票");
        activity = this;
        back.setOnClickListener(this);
        locationStart.setOnClickListener(this);
        locationEnd.setOnClickListener(this);
        checkLine1.setOnClickListener(this);
        add.setOnClickListener(this);
        low.setOnClickListener(this);
        onceTicketAbout.setOnClickListener(this);
        perNumber.setText(number+"");
        if (getIntent()!=null){
            travelLineBean = (TravelLineBean) getIntent().getSerializableExtra("lineNum");
            presenter.startLoadStations(travelLineBean.getNumber());
            drewDatePicket();
            drewDialog();
        }
        String[] thisTime = TimeRender.getFullDateStr9();
        month_time.setText(thisTime[0]+"年"+thisTime[1]+"月");
        perNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                ChangeToatalMoney();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    public void initData() {
        submit.setOnClickListener(new TaylorClickLinstener() {
            @Override
            public void onMultiClick(View v) {
                //购买单程票
                if (checkTickets.size() < 1) {
                    ToastJoe.getmToastJoe().ToastLongShow(BuyTicketsActivity.this, null, "请选择乘车日期");
                } else if (price.equals("0")) {
                    presenter.startGetPrice(travelLineBean.getNumber(), chooseStartId, chooseEndId);
                } else {
                    String statTime = "";
                    String endTime = "";
                    for (int j = 0; j < stations.size(); j++) {
                        if (chooseStartId.equals(stations.get(j).getStationId())){
                            statTime = stations.get(j).getTime();
                        }
                        if (chooseEndId.equals(stations.get(j).getStationId())){
                            endTime = stations.get(j).getTime();
                        }
                    }
                    List<String> datas = new ArrayList<>();
                    for (int i = 0; i < checkTickets.size(); i++) {
                        datas.add(checkTickets.get(i).getTotalDate());
                    }
                    presenter.doBuyTicketOnOnce(BuyTicketsActivity.this,SPUtils.getUserPhone(getApplicationContext())
                            ,travelLineBean.getNumber(), chooseStartId
                            ,chooseEndId,datas,statTime,endTime,number);
                }
            }
        });

    }


    @Override
    protected BasePresenter bindPresenter() {
        return presenter;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.head_left:
                finish();
                break;
            case R.id.location_start:
                if (startStationsId.size()==0 && stations.size()>0){
                    for (int i = 0; i < stations.size()-1; i++) {
                        startStations.add(stations.get(i).getName());
                        startStationsId.add(stations.get(i).getStationId());
                    }
                }
                tvStart.show(v);
                break;
            case R.id.location_end:
                if (chooseStartId.equals("")){
                    ToastJoe.getmToastJoe().ToastLongShow(BuyTicketsActivity.this,null,"请先选择出发站");
                }else {
                    ClearEndStation();
                    tvEnd.show(v);
                }
                break;
            case R.id.ticket_once_about:
                Intent intent = new Intent(BuyTicketsActivity.this,InfoActivity.class);
                intent.putExtra("type",1);
                startActivity(intent);
                break;

            case R.id.add:
                if (checkTickets.size()>0) {
                    if (number > 1) {
                        perNumber.setText((--number) + "");
                    }
                }else{
                    ToastJoe.getmToastJoe().ToastShow(BuyTicketsActivity.this,null,"请先选择乘车时间");
                }
                break;
            case R.id.low:
                if (checkTickets.size()>0) {
                    if (number<10) {
                        perNumber.setText((++number) + "");
                    }else{
                        ToastJoe.getmToastJoe().ToastShow(BuyTicketsActivity.this,null,"一次最多选择十人哦~");
                    }
                }else{
                    ToastJoe.getmToastJoe().ToastShow(BuyTicketsActivity.this,null,"请先选择乘车时间");
                }
                break;
        }
    }

    private void ClearEndStation(){
        endStations.clear();
        endStationsId.clear();
        for (int i = postition+1; i < stations.size(); i++) {
            endStations.add(stations.get(i).getName());
            endStationsId.add(stations.get(i).getStationId());
        }
    }

    @Override
    public void showLoading() {
        if (waitDialog==null)
            waitDialog = new LoadingDialog(BuyTicketsActivity.this);
        if (!waitDialog.isShowing())
            waitDialog.show();
    }

    @Override
    public void closeLoading() {
        if (waitDialog!=null && waitDialog.isShowing())
            waitDialog.dismiss();
    }

    @Override
    public void loadError(String msg) {
        ToastJoe.getmToastJoe().ToastLongShow(BuyTicketsActivity.this,null,msg);
    }

    @Override
    public void loadedData(List<StationBean> s) {
        stations  = s;
        if (startStationsId.size()==0 && stations.size()>0){
            for (int i = 0; i < stations.size()-1; i++) {
                startStations.add(stations.get(i).getName());
                startStationsId.add(stations.get(i).getStationId());
            }
            for (int i = postition+1; i < stations.size(); i++) {
                endStations.add(stations.get(i).getName());
                endStationsId.add(stations.get(i).getStationId());
            }
        }
        chooseStart = stations.get(0).getName();
        chooseStartId = stations.get(0).getStationId();
        chooseEnd = stations.get(stations.size()-1).getName();
        chooseEndId = stations.get(stations.size()-1).getStationId();
        start.setText(chooseStart);
        end.setText(chooseEnd);
        presenter.startGetPrice(travelLineBean.getNumber(), chooseStartId, chooseEndId);
    }

    @Override
    public void loadeIsCanBuyTicket(String isOk,DateJoe dateJoe,int pos) {
        if (isOk.equals("false")){
            dateJoe.isCheck = false;
            adapter.notifyItemChanged(pos,dateJoe);
        }
    }

    @Override
    public void loadeIsCanBuyTicket1(int isOk, DateJoe dateJoe, int pos ,String count) {
        if (TimeRender.compareTime666(dateJoe.getTotalDate()+" "+stations.get(0).getTime())){
            showDialog("温馨提示","该班次已发车，发车后无法退票，请确认是否购买",dateJoe,pos,count);
        }else {
            dateJoe.setCount(count);
            adapter.notifyItemChanged(pos, dateJoe);
            checkTickets.add(dateJoe);
            ChangeToatalMoney();
        }

    }

    private void ChangeToatalMoney(){
        if (price!=null && !price.equals("")) {
            int num = checkTickets.size()*number;
            total_price.setText("￥" + (num * Double.valueOf(price)));
            checkTicketNumber.setText("共选择" + num + "张车票");
        }else{
            ToastJoe.getmToastJoe().ToastLongShow(BuyTicketsActivity.this,null,"线路信息错误");
            finish();
        }

    }

    @Override
    public void loadeBuyPrice(String price) {
        if (travelLineBean.getIsUserAllPrice()!=null){
            if(travelLineBean.getIsUserAllPrice().equals("0")){
                if (price!=null && !price.equals("0")){
                    this.price = price;
                }
            }else{
                this.price = travelLineBean.getPrice();
            }
            for (int i = 0; i < dateJoes.size(); i++) {
                dateJoes.get(i).setPrice("￥"+this.price);
            }
//            adapter.setData(dateJoes);
//            adapter.notifyDataSetChanged();
            presenter.getDays(travelLineBean.getNumber());
            ChangeToatalMoney();
        }

    }

    @Override
    public void loadeIsBuySuccess(RequestBean requestBea) {
        closeLoading();
        if (requestBea.getReCode().equals("201")){
            Intent intent = new Intent(BuyTicketsActivity.this,PayActivity.class);
            Bundle bundle = new Bundle();
            bundle.putInt("position",0);
            bundle.putSerializable("bean",travelLineBean);
            bundle.putSerializable("list", (Serializable) checkTickets);
            bundle.putString("start", chooseStart+"("+stations.get(startStationsId.indexOf(chooseStartId)).getTime()+")");
            bundle.putString("end", chooseEnd+"("+stations.get(endStationsId.indexOf(chooseEndId)).getTime()+")");
            bundle.putString("price",price);
            bundle.putSerializable("pay",requestBea);
            bundle.putInt("number",number);
            intent.putExtras(bundle);
            startActivity(intent);
        }else if (requestBea.getReCode().equals("202")){
            // TODO: 2018/7/2 这里跳转到车票界面展示周期票
            ToastJoe.getmToastJoe().ToastShowRight(getApplicationContext(),null,"购买成功");
            Intent intent = new Intent(BuyTicketsActivity.this, MyTicketActivity.class);
            startActivity(intent);
            BuyTicketsActivity.activity.finish();
            finish();
        }else{
            ToastJoe.getmToastJoe().ToastLongShow(this,null,requestBea.getCodeTxt());
        }
    }

    @Override
    public void loadBuyMonthSuccess(RequestBean requestBean) {
        closeLoading();
        if (requestBean.getReCode().equals("201")){
            Intent intent = new Intent(BuyTicketsActivity.this,PayActivity.class);
            Bundle bundle = new Bundle();
            bundle.putInt("position",1);
            bundle.putSerializable("bean",travelLineBean);
            bundle.putSerializable("list", (Serializable) checkTickets);
            bundle.putString("start", stations.get(0).getName()+"("+stations.get(0).getTime()+")");
            bundle.putString("end", stations.get(stations.size()-1).getName()+"("+stations.get(stations.size()-1).getTime()+")");
            bundle.putString("price",total_price.getText().toString().replace("￥",""));
            bundle.putSerializable("pay",requestBean);
            bundle.putInt("number",number);
            intent.putExtras(bundle);
            startActivity(intent);
        }else{
            ToastJoe.getmToastJoe().ToastLongShow(this,null,requestBean.getCodeTxt());
        }
    }

    @Override
    public void loadDays(List<YuPiaoBean> beans) {
        for (int i = 0; i < dateJoes.size(); i++) {
            dateJoes.get(i).isShow = false;
            dateJoes.get(i).setCount(beans.get(0).getAllSeat()+"");
            if (!dateJoes.get(i).isWeek()){
                dateJoes.get(i).isShow = true;
            }
            //这里判断是否是通勤，开通周末购票
            if (travelLineBean!=null && travelLineBean.getType()!=null &&  !travelLineBean.getType().equals("CommuteLine")){
                dateJoes.get(i).setCommuting(true);
                dateJoes.get(i).isShow = true;
                dateJoes.get(i).setWeek(false);
                if (!TimeRender.compareTime3(dateJoes.get(i).getTotalDate())) {
                    dateJoes.get(i).setWeek(true);
                    dateJoes.get(i).isShow = false;
                }
            }
            for (int j = 0; j < beans.size(); j++) {
                String[] msgs = beans.get(j).getBusTime().split("\\.");
                if (msgs.length>2 && Integer.valueOf(msgs[2])==Integer.valueOf(dateJoes.get(i).getDay())){
                    dateJoes.get(i).setCount(beans.get(j).getYuPiao() + "");
                    if (dateJoes.get(i).isWeek()) {//如果遇到周末有时间日期的返回，则开放周末购票
                        dateJoes.get(i).setWeek(false);
                        dateJoes.get(i).isShow = true;
                    }
                    if (beans.get(j).getYuPiao()>0) {
                        dateJoes.get(i).setSlodOut(false);
                        dateJoes.get(i).isShow = true;
                    }else{
                        dateJoes.get(i).setSlodOut(true);
                        dateJoes.get(i).isShow = false;
                    }
                    if (TimeRender.compareTime666(dateJoes.get(i).getTotalDate()+" "+stations.get(stations.size()-1).getTime())){
                        dateJoes.get(i).isShow = false;
                        dateJoes.get(i).setWeek(true);
                    }
                }
            }
        }
//        adapter.setData(dateJoes);
//        adapter.notifyDataSetChanged();
        presenter.getBuyeds(travelLineBean.getNumber(),SPUtils.getUserPhone(getApplicationContext()));
    }

    @Override
    public void getBuyeds(List<BuyedBean> beans) {
        if (beans!=null) {
            for (int i = 0; i < dateJoes.size(); i++) {
                for (int j = 0; j < beans.size(); j++) {
                    String[] msgs = beans.get(j).getDay().split("\\.");
                    if (msgs.length > 2 && Integer.valueOf(msgs[2]) == Integer.valueOf(dateJoes.get(i).getDay())) {
                        dateJoes.get(i).setBuyed(true);
                    }
                }
            }
        }
        adapter.setData(dateJoes);
        adapter.notifyDataSetChanged();
    }

    private AlertDialog build;
    private void showDialog(final String title, final String msg, final DateJoe dateJoe, final int pos, final String count){
        build = new AlertDialog.Builder(BuyTicketsActivity.this,R.style.AlertDialogStyle).create();
        //自定义布局
        View view = getLayoutInflater().inflate(R.layout.dialog_nopay_layout, null);
        build.setView(view, 0, 0, 0, 0);
        build.show();
        //得到当前显示设备的宽度，单位是像素
        int width = getWindowManager().getDefaultDisplay().getWidth();
        //得到这个dialog界面的参数对象
        WindowManager.LayoutParams params = build.getWindow().getAttributes();
        //设置dialog的界面宽度
        params.width = width-(width/3);
        //设置dialog高度为包裹内容
        params.height =  WindowManager.LayoutParams.WRAP_CONTENT;
        //设置dialog的重心
        params.gravity = Gravity.CENTER;
        build.getWindow().setAttributes(params);
        build.setCanceledOnTouchOutside(false);
        build.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {

            }
        });
        TextView titleTv = view.findViewById(R.id.message);
        TextView messageTv = view.findViewById(R.id.message1);
        titleTv.setText(title);
        messageTv.setText(msg);
        Button btn_sure = view.findViewById(R.id.yes);
        Button btn_cancel = view.findViewById(R.id.no);
        btn_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dateJoe.setCount(count);
                adapter.notifyItemChanged(pos, dateJoe);
                checkTickets.add(dateJoe);
                ChangeToatalMoney();
                build.dismiss();
            }
        });
        btn_cancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                dateJoe.isCheck = false;
                adapter.notifyItemChanged(pos,dateJoe);
                build.dismiss();
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(waitDialog!=null){
            waitDialog.dismiss();
            waitDialog=null;
        }
        if (build!=null){
            build =null;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("购票");
        MobclickAgent.onResume(this); //统计时长

    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("购票");
        MobclickAgent.onPause(this);
    }
}
